import win32gui

def cleanup():
    try:
        win32gui.ShowWindow(mycmd, 1)  # 显示命令行窗口
    except:
        pass

def enum_windows_callback(hwnd, hwnds):
    class_name = win32gui.GetClassName(hwnd)
    name = win32gui.GetWindowText(hwnd)
    try:
        if (
            class_name == "ConsoleWindowClass"
            and win32gui.IsWindowVisible(hwnd)
            and "start" in name[-9:]
        ):
            hwnds.append(hwnd)
    except:
        pass
    return True


def list_handles():
    hwnds = []
    win32gui.EnumWindows(enum_windows_callback, hwnds)
    hwnds.append(0)
    return hwnds

mycmd = list_handles()[0]

def CMD_Enable(Value):
    try:
        # if win32gui.IsWindowVisible(mycmd):
        #     win32gui.ShowWindow(mycmd, Value)  # 隐藏命令行窗口
        # elif win32gui.IsWindowVisible(mycmd) is False:
        win32gui.ShowWindow(mycmd, Value)  # 显示命令行窗口
    except:
        pass